/*jslint browser: true, cap: false, glovar: true, passfail: false, undef: false, white: false */
/*global SONGHAY YAHOO */

(function(){

    //Module-level members:
    var root = SONGHAY;

    SONGHAY.ui.EventCore =
    {
        setEvent:function(oEventMetaData, oWidget, oEventPassThroughObject)
        {
            if(oEventMetaData && (oEventMetaData instanceof Array) && oWidget)
            {
                //An array of event meta-data was defined and found:
                for(var i = 0; i < oEventMetaData.length; i++)
                {
                    if(oEventPassThroughObject &&
                        oEventPassThroughObject instanceof Array &&
                        oEventPassThroughObject[i])
                    {
                        //An array of pass-through objects were passed:
                        YAHOO.util.Event.addListener(
                            oWidget,
                            oEventMetaData[i].eventType,
                            oEventMetaData[i].eventCallback,
                            oEventPassThroughObject[i]
                       );
                    }
                    else if(oEventPassThroughObject)
                    {
                        //A single pass-through objects was passed:
                        YAHOO.util.Event.addListener(
                            oWidget,
                            oEventMetaData[i].eventType,
                            oEventMetaData[i].eventCallback,
                            oEventPassThroughObject);
                    }
                    else
                    {
                        YAHOO.util.Event.addListener(
                            oWidget,
                            oEventMetaData[i].eventType,
                            oEventMetaData[i].eventCallback,
                            oEventMetaData[i].eventPassThroughObject);
                    }
                }
            }
            else if(oEventMetaData && oWidget)
            {
                //Event meta-data was defined and found:
                if(oEventPassThroughObject)
                {
                    //A single pass-through objects was passed:
                    YAHOO.util.Event.addListener(
                        oWidget,
                        oEventMetaData.eventType,
                        oEventMetaData.eventCallback,
                        oEventPassThroughObject);
                }
                else
                {
                    YAHOO.util.Event.addListener(
                        oWidget,
                        oEventMetaData.eventType,
                        oEventMetaData.eventCallback,
                        oEventMetaData.eventPassThroughObject);
                }
            }
        },

        setEventByMetaDataId:function(vEventMetaDataId,oWidget)
        {
            var oEventMetaData = root.ui.Events[vEventMetaDataId];
            var oEventPassThroughObject = (arguments.length > 2) ? arguments[2] : null;

            self.setEvent(oEventMetaData,oWidget,oEventPassThroughObject);
        },

        setEventByWidgetId:function(vWidgetId)
        {
            var oEventMetaData = root.ui.Events[vWidgetId];
            var oWidget = YAHOO.util.Dom.get(vWidgetId);
            var oEventPassThroughObject = (arguments.length > 1) ? arguments[1] : null;

            self.setEvent(oEventMetaData,oWidget,oEventPassThroughObject);
        },

        setKeyListenerEvent:function(vEventMetaDataId, oWidget)
        {
            var oListenerMeta = root.ui.Events[vEventMetaDataId];

            if(oListenerMeta)
            {
                 oListenerMeta.keyListener = new YAHOO.util.KeyListener(oWidget, oListenerMeta.keysMeta, oListenerMeta.eventCallback);
            }

            return oListenerMeta;
        }
    };

    //Module-level "this":
    var self = SONGHAY.ui.EventCore;

})();
